package in.smartbillpay.web.dao;

import java.util.List;
import in.smartbillpay.web.models.Authority;

import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional(readOnly = true)
public class AuthorityDaoImpl implements AuthorityDao {

	
	 protected HibernateTemplate template;

	    /**
	     * Sets Hibernate session factory.
	     */
	 @Autowired
	    public void setSessionFactory(SessionFactory sessionFactory) {
	        template = new HibernateTemplate(sessionFactory);
	    }
	

	public Authority getById(int id){
		return template.load(Authority.class, id); 
	}
	
	
	public List<Authority> getAll(){
		return template.loadAll(Authority.class);
	}
	
    @Transactional(readOnly = false, propagation = Propagation.NESTED)
	public Authority save(Authority authority) {
    	template.saveOrUpdate(authority);
    	return authority;		
	}

	public void delete(Authority authority) {
		template.delete(authority, LockMode.NONE);		
	}

	@SuppressWarnings("unchecked")
	public List<Authority> getByName(String authorityName) {
		String query = "from Authority where name=?";
		return template.find(query,authorityName);
	}
}
